package com.wehealth.three.lib_common.utils;

import android.annotation.SuppressLint;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@SuppressLint("SimpleDateFormat")
public class DateUtil {

    private static final long INTERVAL_IN_MILLISECONDS = 30 * 1000;
    public static SimpleDateFormat sdf_yyyy_MM_dd_EEE = new SimpleDateFormat("yyyy-MM-dd EEE");// EEE代表星期几
    public static SimpleDateFormat sdf_m = new SimpleDateFormat("mm");
	public static SimpleDateFormat sdfsign = new SimpleDateFormat("yyyyMMddHH");
    public static SimpleDateFormat sdf_yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat sdf_yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
    public static SimpleDateFormat sdf_yyyy = new SimpleDateFormat("yyyy");
	public static SimpleDateFormat sdf_MM = new SimpleDateFormat("MM");
    public static SimpleDateFormat sdf_yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
	public static SimpleDateFormat sdf_yyyy_MM_dd_HH_mm_ss_ = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    public static SimpleDateFormat sdf_yyyy_MM_dd_HHmm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static SimpleDateFormat sdf_yyyy_MM_dd_HHmm_EEE = new SimpleDateFormat("yyyy-MM-dd HH:mm EEE");
    public static SimpleDateFormat sdf_yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat sdf_yyyy_MM_dd_HH_mm_ss_S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    public static SimpleDateFormat sdf_s = new SimpleDateFormat("ss");
    public static SimpleDateFormat sdf_H = new SimpleDateFormat("HH");
	public static SimpleDateFormat sdf_D = new SimpleDateFormat("dd");
    public static SimpleDateFormat sdf_EEE = new SimpleDateFormat("EEE");// EEE代表星期几
    public static SimpleDateFormat sdf_HHmm = new SimpleDateFormat("HHmm");
    public static SimpleDateFormat sdf_HH_mm = new SimpleDateFormat("HH:mm");
	public static SimpleDateFormat sdf_MM_dd_HH_mm_ss = new SimpleDateFormat("MM-dd HH:mm:ss");
	public static SimpleDateFormat sdf_HHmmss = new SimpleDateFormat("HHmmss");
    

    /**
     * 获取某年某月有多少天
     * @param year
     * @param month
     * @return
     */
    public static int getDayOfMonth(int year,int month){
        Calendar c = Calendar.getInstance();
        c.set(year, month, 0); //输入类型为int类型
        return c.get(Calendar.DAY_OF_MONTH);
    }

    public static boolean isCloseEnough(long time1, long time2) {
        // long time1 = date1.getTime();
        // long time2 = date2.getTime();
        long delta = time1 - time2;
        if (delta < 0) {
            delta = -delta;
        }
        return delta < INTERVAL_IN_MILLISECONDS;
    }


    public static Date StringToDate(String dateStr, String formatStr) {
        DateFormat format = new SimpleDateFormat(formatStr);
        Date date = null;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    /**
     * 
     * @param timeLength Millisecond
     * @return
     */
    public static String toTime(int timeLength) {
        timeLength /= 1000;
        int minute = timeLength / 60;
        int hour = 0;
        if (minute >= 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        int second = timeLength % 60;
        // return String.format("%02d:%02d:%02d", hour, minute, second);
        return String.format("%02d:%02d", minute, second);
    }
    /**
     * 
     * @param timeLength second
     * @return
     */
    public static String toTimeBySecond(int timeLength) {
//      timeLength /= 1000;
        int minute = timeLength / 60;
        int hour = 0;
        if (minute >= 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        int second = timeLength % 60;
        // return String.format("%02d:%02d:%02d", hour, minute, second);
        return String.format("%02d:%02d", minute, second);
    }
     
    


    public static String getTimestampStr() {
        return Long.toString(System.currentTimeMillis());
    }
    
    /**最近一周**/
   	public static Date getLastWeek(){
   		Calendar calendar1 = Calendar.getInstance();
   		calendar1.add(GregorianCalendar.DATE, -6);
   		Date date = calendar1.getTime();
		String s = sdf_yyyy_MM_dd.format(date);
		String start = s+" 00:00";
		Date startDate = null;
		try {
			startDate = sdf_yyyy_MM_dd_HHmm.parse(start);
		} catch (ParseException e) {
			e.printStackTrace();
			startDate = calendar1.getTime();
		}
   		return startDate;
   	}



   	/**
   	 * 将小时转换成天（周）的小数
   	 * @return
   	 */
   	public static double dayOfWeekToDouble(Date d){
   		String H = sdf_H.format(d);
   		String m = sdf_m.format(d);
   		String s = sdf_s.format(d);
   		double sd = Double.valueOf(s);
   		double md = Double.valueOf(m);
   		double hd = Double.valueOf(H);
   		BigDecimal mb = new BigDecimal(md/60);
   		BigDecimal sb = new BigDecimal(sd/(60*60));
   		double df1 = mb.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
   		double df2 = sb.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
   		BigDecimal hb = new BigDecimal((hd+df1+df2)/48);
   		double f2 = hb.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
   		return f2;
   	}

   	public static String parseTime(String times) {
		String timeStr = "";
		if (times.contains(",")) {
			String[] tims = times.split(",");
			if (tims.length==2) {
				timeStr = sdf_HH_mm.format(new Date(Long.valueOf(tims[0])));
				timeStr += ","+sdf_HH_mm.format(new Date(Long.valueOf(tims[1])));
			}
			if (tims.length==3) {
				timeStr = sdf_HH_mm.format(new Date(Long.valueOf(tims[0])));
				timeStr += ","+sdf_HH_mm.format(new Date(Long.valueOf(tims[1])));
				timeStr += ","+sdf_HH_mm.format(new Date(Long.valueOf(tims[2])));
			}
			if (tims.length==4) {
				timeStr = sdf_HH_mm.format(new Date(Long.valueOf(tims[0])));
				timeStr += ","+sdf_HH_mm.format(new Date(Long.valueOf(tims[1])));
				timeStr += ","+sdf_HH_mm.format(new Date(Long.valueOf(tims[2])));
				timeStr += ","+sdf_HH_mm.format(new Date(Long.valueOf(tims[3])));
			}
		}else {
			timeStr = sdf_HH_mm.format(new Date(Long.valueOf(times)));
		}
		return timeStr;
	}
}